package cn.itcast.dao;

import cn.itcast.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MybatisDemo1 {
    private SqlSession sqlSession;

    @Before
    public void before() throws Exception {
        //1.读取核心配置文件,resources
        InputStream is = Resources.getResourceAsStream("Sql.xml");
        //2.构建sqlsessionfactory工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //3.获取sqlsession对象
        sqlSession = sqlSessionFactory.openSession();

    }

    @After
    public void after() throws Exception {
        //5.提交事务!!!!!!!!!!!!!!!!!!!!!!更新操作一定要记得提交事务 亚麻跌~~~~~~
        sqlSession.commit();
        //6.释放资源
        sqlSession.close();
    }


    @Test
    /*查询所有用户*/
    public void findAll() throws Exception {
        //4.执行映射配置文件中的sql语句,并接收结果
        List<User> userList = sqlSession.selectList("userMapper.findAll");
    }

    @Test
    /*根据id查询用户*/
    public void findById() throws Exception {
        User o = sqlSession.selectOne("userMapper.findById", 1);
        System.out.println(o);
    }

    @Test
    /*根据id查询用户*/
    public void findByUserId() throws Exception {
        User u = new User();
        u.setId(10);
        User o = sqlSession.selectOne("userMapper.findByUserId", u);
        System.out.println(o);
    }

    @Test
    /*根据id查询用户*/
    public void findByMap() throws Exception {
        Map map = new HashMap();
        map.put("uid", 24);
        User o = sqlSession.selectOne("userMapper.findByMap", map);
        System.out.println(o);
    }

    @Test
    /*模糊查询1*/
    public void findLikeName() throws Exception {
        List<User> users = sqlSession.selectList("userMapper.findLikeName", "%明%");
        for (User user : users) {
            System.out.println(user);
        }
    }

    @Test
    /*模糊查询2*/
    public void findLikeName1() throws Exception {
        List<User> users = sqlSession.selectList("userMapper.findLikeName1", "张");
        for (User user : users) {
            System.out.println(user);
        }
    }

    @Test
    /*统计人员数量*/
    public void findCount() throws Exception {
        int count = sqlSession.selectOne("userMapper.findCount");
        System.out.println(count);
    }

    @Test
    /*分组统计男女的数量*/
    public void findBySex() throws Exception {
        Map<String, Long> map = new HashMap<String, Long>();
        List<Map<String, Object>> lists = sqlSession.selectList("userMapper.findBySex");
        for (Map list : lists) {
            String key = (String) list.get("sex");
            Long value = (long) list.get("zzz");
            System.out.println(key + "," + value);
            map.put(key, value);
        }
        System.out.println(map);
    }

    @Test
    /*添加一个用户*/
    public void insert() throws Exception {

        User user = new User();
        user.setId(5);
        user.setUsername("王舞");
        user.setSex("女");
        user.setAddress("合肥");

        sqlSession.insert("userMapper.insert", user);
    }

    @Test
    /*添加一个用户*/
    public void update() throws Exception {
        User user = new User();
        user.setId(5);
        user.setUsername("王屋");

        sqlSession.update("userMapper.update", user);
    }

    @Test
    /*删除一个用户*/
    public void delete() throws Exception {
        User user  = new User();
        user.setId(2);
        sqlSession.delete("userMapper.delete",user);

    }
}
